home *** CD-ROM | disk | FTP | other *** search
Oberon Text | 1995-12-01 | 6.2 KB | 173 lines | [TEXT/.Ob4] |
- Syntax10.Scn.Fnt
- Syntax10b.Scn.Fnt
- Syntax10i.Scn.Fnt
- InfoElems
- Alloc
- Syntax10.Scn.Fnt
- StampElems
- Alloc
- 1 Dec 95
- "Title": OpenElems
- "Author": Christoph Steindl (CS)
- "Abstract": OpenElems perform a generic open command on the selected item. Depending on the suffix of the
- entry different commands are called. The items are collected automatically from the current directory.
- "Keywords": Opening files
- "Version": 1.0
- "From": 22.05.95 15:39:15
- "Until":
- "Changes": no changes
- "Hints": You can insert new suffix mappings by calling OpenElems.Add pattern command, can also be used to override
- the default mapping, i.e. OpenElems.Add *.Text MyEdit.Open will cause *.Text files to be opened with MyEdit.
- The predefined mappings are taken from the file Dir.Menu.Text. The syntax for entries in Dir.Menu.Text is described
- in Dir.Text.
- MODULE OpenElems; (** CS
- Standard mapping of patterns to commands are taken from Dir.Menu.Text *)
- IMPORT Viewers, Texts, TextFrames, Oberon, PopupElems, Directories, Display, Strings, In;
- OpenElem* = POINTER TO OpenElemDesc;
- OpenElemDesc* = RECORD (PopupElems.ElemDesc)
- END;
- AutoDirElem* = POINTER TO AutoDirElemDesc;
- AutoDirElemDesc* = RECORD (OpenElemDesc)
- END;
- Str64 = ARRAY 64 OF CHAR;
- Entry = POINTER TO EntryDesc;
- EntryDesc = RECORD
- pat: ARRAY 10 OF CHAR;
- cmd: Str64;
- next: Entry
- END;
- w: Texts.Writer;
- map: Entry;
- cnt: INTEGER;
- pattern: ARRAY 32 OF CHAR;
- PROCEDURE ReadName (t: Texts.Text; pos: LONGINT; VAR name: ARRAY OF CHAR);
- VAR r: Texts.Reader; ch: CHAR; i: INTEGER;
- BEGIN
- Texts.OpenReader(r, t, pos); Texts.Read(r, ch); i := 0;
- IF ch = '"' THEN Texts.Read(r, ch);
- WHILE ~r.eot & (ch # '"') DO name[i] := ch; INC(i); Texts.Read(r, ch) END
- ELSE
- WHILE ~r.eot & (ch > " ") DO name[i] := ch; INC(i); Texts.Read(r, ch) END
- END;
- name[i] := 0X
- END ReadName;
- PROCEDURE Lookup(VAR name, cmd: ARRAY OF CHAR);
- VAR e: Entry;
- BEGIN
- e := map; WHILE (e # NIL) & ~Strings.Match(name, e.pat) DO e := e.next END;
- IF e # NIL THEN COPY(e.cmd, cmd) ELSE COPY("Edit.Open", cmd) END
- END Lookup;
- PROCEDURE Exec (e: OpenElem; f: Display.Frame; pos: LONGINT);
- VAR m: TextFrames.UpdateMsg; res: INTEGER; name: ARRAY 256 OF CHAR;
- par: Oberon.ParList; cmd: ARRAY 64 OF CHAR; dir: Directories.Directory;
- BEGIN
- ReadName(e.menu, pos, name);
- dir := Directories.This(name);
- IF dir # NIL THEN
- Directories.Change(dir.path)
- ELSE
- Lookup(name, cmd);
- NEW(par); par.frame := f; par.text := e.menu; par.pos := pos;
- Oberon.Call(cmd, par, FALSE, res);
- IF res # 0 THEN Texts.WriteString(w, " -- failed"); Texts.WriteLn(w); Texts.Append(Oberon.Log, w.buf) END
- END Exec;
- PROCEDURE Handle* (e: Texts.Elem; VAR m: Texts.ElemMsg);
- VAR e1: OpenElem;
- BEGIN
- WITH e: OpenElem DO
- WITH
- m: Texts.CopyMsg DO
- IF m.e = NIL THEN NEW (e1); m.e := e1 END;
- PopupElems.Handle(e, m)
- | m: Texts.IdentifyMsg DO
- m.mod := "OpenElems"; m.proc := "Alloc"
- | m: PopupElems.ExecMsg DO Exec(e, m.frame, m.pos)
- ELSE PopupElems.Handle(e, m)
- END
- END Handle;
- PROCEDURE Alloc*;
- VAR e: OpenElem;
- BEGIN
- NEW(e); e.handle := Handle; Texts.new := e
- END Alloc;
- PROCEDURE Insert*;
- VAR e: OpenElem; insert: TextFrames.InsertElemMsg; s: Texts.Scanner;
- BEGIN
- NEW(e); e.handle := Handle; e.small := TRUE;
- Texts.OpenScanner(s, Oberon.Par.text, Oberon.Par.pos); Texts.Scan(s);
- IF ~(s.class IN {Texts.Name, Texts.String}) THEN s.s := "OpenElem" END;
- COPY(s.s, e.name);
- e.menu := TextFrames.Text(""); PopupElems.MeasureMenu(e);
- insert.e := e; Viewers.Broadcast(insert)
- END Insert;
- PROCEDURE AddEntry (pattern, cmd: ARRAY OF CHAR);
- VAR e: Entry;
- BEGIN
- NEW(e);
- COPY(pattern, e.pat); COPY(cmd, e.cmd);
- IF (e.pat # "") & (e.cmd # "") THEN
- e.next := map; map := e
- END AddEntry;
- PROCEDURE Add*; (* pattern command *)
- VAR pattern, cmd: Str64;
- BEGIN
- In.Open; In.Name(pattern); In.Name(cmd);
- AddEntry(pattern, cmd)
- END Add;
- PROCEDURE ListProc (d: Directories.Directory; name: ARRAY OF CHAR; isDir: BOOLEAN; VAR continue: BOOLEAN);
- BEGIN
- IF Strings.Match(name, pattern) OR isDir THEN Texts.WriteString (w, name); Texts.WriteLn (w); INC(cnt) END
- END ListProc;
- PROCEDURE CollectFiles (e: OpenElem);
- BEGIN
- e.menu := TextFrames.Text(""); cnt := 0; COPY(e.name, pattern);
- Directories.Enumerate(Directories.Current(), ListProc);
- IF cnt = 1 THEN Texts.WriteLn(w) END; (* prohibit AutoDirElems that do not pop up because they only contain one item *)
- IF w.buf.len = 0 THEN Texts.WriteString (w, "no files found"); Texts.WriteLn (w); Texts.WriteLn (w) END;
- Texts.Append(e.menu, w.buf); PopupElems.MeasureMenu(e)
- END CollectFiles;
- PROCEDURE HandleAutoDir* (e: Texts.Elem; VAR m: Texts.ElemMsg);
- VAR e1: AutoDirElem;
- BEGIN
- WITH e: AutoDirElem DO
- WITH m: Texts.CopyMsg DO
- IF m.e = NIL THEN NEW (e1); m.e := e1 END;
- Handle(e, m)
- | m: Texts.IdentifyMsg DO
- m.mod := "OpenElems"; m.proc := "AllocAutoDir"
- | m: TextFrames.TrackMsg DO
- IF 1 IN m.keys THEN CollectFiles (e); Handle (e, m) END
- ELSE Handle(e, m)
- END
- END HandleAutoDir;
- PROCEDURE AllocAutoDir*;
- VAR e: AutoDirElem;
- BEGIN
- NEW(e); e.handle := HandleAutoDir; Texts.new := e
- END AllocAutoDir;
- PROCEDURE InsertAutoDir*;
- VAR e: AutoDirElem; insert: TextFrames.InsertElemMsg;
- BEGIN
- NEW(e); e.handle := HandleAutoDir; e.small := TRUE; e.name := "*";
- e.menu := TextFrames.Text(""); PopupElems.MeasureMenu(e);
- insert.e := e; Viewers.Broadcast(insert)
- END InsertAutoDir;
- PROCEDURE NoNotify (t: Texts.Text; op: INTEGER; beg, end: LONGINT);
- END NoNotify;
- PROCEDURE Init;
- VAR cmd, pattern: Str64; scratch: Texts.Text; s: Texts.Scanner;
- BEGIN
- NEW(scratch); Texts.Open(scratch, "Dir.Menu.Text"); scratch.notify := NoNotify;
- Texts.OpenScanner(s, scratch, 0);
- REPEAT Texts.Scan(s) UNTIL s.eot OR (s.line # 0);
- WHILE s.class = Texts.String DO
- COPY(s.s, pattern); Texts.Scan(s);
- IF s.class IN {Texts.Name, Texts.String} THEN COPY(s.s, cmd); Texts.Scan(s) END;
- AddEntry(pattern, cmd)
- END Init;
- BEGIN
- Texts.OpenWriter(w);
- Init
- END OpenElems.Insert
- OpenElems.InsertAutoDir
-